# coding=utf-8

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from models import models
from models.settings import Menu
from utils.enums import Struct
from utils.htmlutils import setXmlType, setXhtmlType
import logging


class MainPage(webapp.RequestHandler):

    def __init__(self):
        logging.info('init ' + str(self.__class__))
        self.struct = Struct()
        self.struct.user = users.get_current_user()
        self.struct.account = models.get_account(self.struct.user)

        self.struct.menu = Menu


    def _init_get(self):
        self.template_file = 'templates/main.html'

        self.struct.style_css = ['main.css',
                                 'headers/header.css',
                                 'footers/footer.css',
                                 'left/left.css',
                                 'main/body.css',
                                 'footers/debug.css']
        self.struct.logo = 'Welcome to the main page'
        self.struct.href_login = users.create_login_url('/')
        self.struct.href_logout = users.create_logout_url('/')

        self.struct.title = 'YooHOo, ' + (self.struct.user
                                          and self.struct.user.nickname()
                                          or '%username%') + ' !'
        self.struct.js = ['debug/debug.js',
                           'left/left.js',
                           'main/body.js']



        self.struct.notes = models.get_notes(self.struct.user)

        #for n in self.struct.notes:
        #    n.note = n.note.replace('\n', '<br>').replace(' ', '_')

    def get(self):
        #setXhtmlType(self.response)
        self._init_get()
        self.response.out.write(template.render(self.template_file, self.struct._compile()))


    def _init_post(self):
        self.note_template = 'templates/main/note.xml'
        self.notes_template = 'templates/main/notes.xml'
        self.setting_template = "templates/main/setting.xml"
#TODO: gather all functionality in this class


    def post(self):
        self._init_post()
        if self.struct.account is None:
            self.response.set_status(403, 'Forbidden')
            return

        action = self.request.get('action')
        if action is None:
            self.response.set_status(400, 'Bad Request')
            return

        setXmlType(self.response)

        if action == 'new':
            self._new()
        elif action == 'edit':
            self._edit()
        elif action == 'apply':
            self._apply()
        elif action == 'remove':
            self._remove()
        elif action == 'menu':
            self._menu()

    def _menu(self):
        menuid = None
        try:
            menuid = int(self.request.get('menuid', '0'))
        except ValueError:
            self.response.set_status(400, 'Bad Request')
            return

        if not menuid in xrange(len(self.struct.menus)):
            self.response.set_status(400, 'Bad Request')
            return

        self.response.out.write(template.render(self.notes_template, self.struct._compile()))

    def _new(self):
        self.struct.note = models.create_note(self.user, 'Title of the new note', 'Sample')
        self.struct.note.edit = True
        self.response.out.write(template.render(self.note_template, self.struct._compile()))

    def _edit(self):
        id = self.request.get('id')
        if not id:
            self.response.set_status(400, 'Bad Request')
            return
        self.struct.note = models.get_note(id)
        if not self.struct.note:
            self.response.set_status(400, 'Bad Request')
            return
        self.struct.note.edit = True
        self.response.out.write(template.render(self.note_template, self.struct._compile()))

    def _remove(self):
        id = self.request.get('id')
        if not id:
            self.response.set_status(400, 'Bad Request')
            return
        models.remove_note(id)



    def _apply(self):
        id = self.request.get('id')
        title = self.request.get('title')
        text = self.request.get('note')

        if not title or not text or not id:
            self.response.set_status(400, 'Bad Request')
            return

        self.struct.note = models.get_note(id)
        if not self.struct.note:
            self.response.set_status(400, 'Bad Request')
            return

        self.struct.note.title = title
        self.struct.note.note = text
        self.struct.note.put()
        self.response.out.write(template.render(self.note_template, self.struct._compile()))








